package tests;

import org.openqa.selenium.By;
import org.testng.Assert;
import pages.ComparePage;
import pages.FoundedProductPage;
import pages.HomePage;
import pages.TicketsPage;
import utils.Log4Test;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Date: 29.05.14
 * Time: 20:06
 * To change this template use File | Settings | File Templates.
 */
public class CommonTestSteps extends BaseTest{
    private static final int FIRST = 1;
    private static final int SECOND = 2;

    private static final By compareLocator = By.xpath("//div[@id='compare-menu']/ul[@class='menu-sort-filter']/li/a[@class='xhr']");

    public void login() {
        HomePage page = new HomePage(driver);

        page.open();
        Assert.assertTrue(page.isOpened(), Log4Test.error("Could not open page"));
    }

    public void findItem(String productName) {
        HomePage page = new HomePage(driver);

        if (page.findItem(productName) == false) {
            Assert.assertTrue(false);
            Log4Test.error("Product isn't found!");
        } else {
            Log4Test.info("found product" + productName);
        }
    }

    public void addToCompare(String item) {
        FoundedProductPage page = new FoundedProductPage(driver);

        page.addToCompare(item);
        Log4Test.info("Added to compare" + item);
    }

    public boolean isComparePageAvailable() {
        FoundedProductPage page = new FoundedProductPage(driver);

        return page.isComparePageAvailable();
    }

    public void comparisonWorks(String prod1, String prod2) {
        FoundedProductPage page = new FoundedProductPage(driver);

        page.compareItems();
        Assert.assertTrue(page.productPresent(prod1));
        Assert.assertTrue(page.productPresent(prod2));
    }

    public void compare() {
        ComparePage page = new ComparePage(driver);

        Map<String, String> parametersOfFirstItem = page.getParameters(FIRST);
        Map<String, String> parametersOfSecondItem = page.getParameters(SECOND);
        Map<String, String> differenceInFirst = new HashMap<String, String>();
        Map<String, String> differenceInSecond = new HashMap<String, String>();
        Map<String, String> realDifFirst = new HashMap<String, String>();
        Map<String, String> realDifSecond = new HashMap<String, String>();

        differenceInFirst = this.getDifferenceOfTwoMaps(parametersOfFirstItem, parametersOfSecondItem);
        differenceInSecond = this.getDifferenceOfTwoMaps(parametersOfSecondItem, parametersOfFirstItem);

        driver.findElement(compareLocator).click();

        realDifFirst = page.getParameters(FIRST);
        realDifSecond = page.getParameters(SECOND);

        for (String name: realDifFirst.keySet()){
            String key = name;
            String realValueOfFirst = realDifFirst.get(name);
            String valueOfFirst = differenceInFirst.get(name);
            String realValueOfSecond = realDifSecond.get(name);
            String valueOfSecond = differenceInSecond.get(name);

            try {
                if (!(realValueOfSecond.length() == 0 && valueOfSecond == null)) {
                    Assert.assertEquals(realValueOfSecond, valueOfSecond);
                }
                if (!(realValueOfFirst.length() == 0 && valueOfFirst == null)) {
                    Assert.assertEquals(realValueOfFirst, valueOfFirst);
                }
            } catch(java.lang.NullPointerException e){
                Log4Test.info("Null pointer catch!");
            }

            Log4Test.info("real key/value of first item: " + key + " :::: " + realValueOfFirst );
            Log4Test.info("key/value of first item: " + key + " :::: " + valueOfFirst );
            Log4Test.info("real key/value of second item: " + key + " :::: " + realValueOfSecond );
            Log4Test.info("key/value of second item: " + key + " :::: " + valueOfSecond );
        }
    }

    public Map<String, String> getDifferenceOfTwoMaps(Map<String, String> first, Map<String, String> second) {
        ComparePage page = new ComparePage(driver);

        Map<String, String> difference = new HashMap<String, String>();

        for (String name: first.keySet()){
            String key = name;
            String valueOfFirst = first.get(name);
            String valueOfSecond = second.get(name);

            if (!valueOfFirst.equals(valueOfSecond)) {
                difference.put(key, valueOfFirst);
            }
        }

        return difference;
    }

    public void buyTickets(int adults, int children, int infants) {
        TicketsPage ticketsPage = new TicketsPage(driver);

        ticketsPage.open();
        ticketsPage.buyTickets(adults,children,infants);
        Assert.assertTrue(ticketsPage.isError(), Log4Test.info("error obtained"));
    }
}
